//
//  XMMessageView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/12.
//

#import "XMMessageView.h"
#import "XMMessageCell.h"

@interface XMMessageView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView *pushMesgView;
@property (nonatomic, strong) UITableView *tbl;
@property (nonatomic, strong) UIView *headView;

@property (nonatomic, strong) NSMutableArray *msgModelArr;

@property (nonatomic, assign) NSInteger lastTimeStamp;
@property (nonatomic, assign) NSInteger lastIndex;

@end

@implementation XMMessageView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"消息";
    self.backBtn.hidden = YES;
    
    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(self.width - KScaleWidth(110), kNavagationBarH - 44, KScaleWidth(110.0), 44) title:@"清除未读" titleColor:kColorHEX(0x333333) image:@""];
    [clearBtn addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:clearBtn];
    
//    [self addSubview:self.pushMesgView];
    
    [self addSubview:self.tbl];
}

//清除记录
- (void)clearAction:(UIButton *)button {
    
}

//开启
- (void)openAction:(UIButton *)button {
    
}

//关闭
- (void)closeAction:(UIButton *)button {
    
}

- (void)setPushStatus:(BOOL)status {
    _pushMesgView.hidden = status;
}

- (void)setContentWithUserModelArr:(NSArray *)userModelArr {
    [self.msgModelArr removeAllObjects];
    [self.msgModelArr addObjectsFromArray:userModelArr];
    [_tbl reloadData];
}

#pragma mark - Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.msgModelArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(77.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XMMessageCellName" forIndexPath:indexPath];
    [cell setContentWithMessageModel:self.msgModelArr[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.delegate && [self.delegate respondsToSelector:@selector(userSelectWithUserId:userName:avatar:)]) {
        XMMessageModel *msgModel = _msgModelArr[indexPath.row];
        [self.delegate userSelectWithUserId:msgModel.toAccount
                                   userName:msgModel.username
                                     avatar:msgModel.avatar];
    }
}

#pragma mark - Init
- (UIView *)pushMesgView {
    if (!_pushMesgView) {
        _pushMesgView = [[UIView alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), kNavagationBarH + KScaleWidth(6.0), self.width - KScaleWidth(16.0)*2, KScaleWidth(42.0))];
        _pushMesgView.backgroundColor = kColorHEX(0xFDF7E7);
        [_pushMesgView setPartCornerWithRectCorner:UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadius:KScaleWidth(21.0)];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(23.0), 0, _pushMesgView.width - KScaleWidth(23.0) - KScaleWidth(120.0), _pushMesgView.height) font:KFontSize(13.0) textColor:kColorHEX(0xE7AF55)];
        titleLabel.text = @"新消息通知已关闭，无法及时接受";
        [_pushMesgView addSubview:titleLabel];
        
        UIButton *openBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(12.0) frame:CGRectMake(_pushMesgView.width - KScaleWidth(43.0) - KScaleWidth(50.0), (_pushMesgView.height - KScaleWidth(24.0))/2, KScaleWidth(50.0), KScaleWidth(24.0)) title:@"去开启" titleColor:kColorHEX(0x333333)];
        [openBtn setRadius:openBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
        [openBtn addTarget:self action:@selector(openAction:) forControlEvents:UIControlEventTouchUpInside];
        [_pushMesgView addSubview:openBtn];
        
        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(_pushMesgView.width - KScaleWidth(10.0) - KScaleWidth(23.0), 0, KScaleWidth(23.0), _pushMesgView.height) image:@"" selectImg:@""];
        [closeBtn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _pushMesgView;
}

- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, /*self.pushMesgView.maxY*/kNavagationBarH, self.width, kScreentHeight - kNavagationBarH - /*_pushMesgView.height -*/ kTabBarH)];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        [_tbl registerClass:[XMMessageCell class] forCellReuseIdentifier:@"XMMessageCellName"];
        _tbl.separatorInset = UIEdgeInsetsMake(0, KScaleWidth(72.0), 0, 1);
        _tbl.separatorColor = kColorHEX(0xF1F1F1);
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
        XMWeakSelf;
        _tbl.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(refreshData)]) {
                [weakSelf.delegate refreshData];
            }
        }];
        _tbl.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(loadMoreWithStartIndex:startTimeStamp:)]) {
                [weakSelf.delegate loadMoreWithStartIndex:weakSelf.lastIndex
                                           startTimeStamp:weakSelf.lastTimeStamp];
            }
        }];
//        _tbl.tableHeaderView = self.headView;
    }
    return _tbl;
}

- (UIView *)headView {
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(110.0))];
        _headView.backgroundColor = kColorHEX(0xffffff);
        
        NSArray *titleArr = @[@"新增关注", @"互动消息", @"系统消息", @"活动消息"];
        NSArray *imgArr = @[@"message_new_atten_icon", @"message_interact_msg_icon", @"message_system_msg_icon", @"message_activity_msg_icon"];
        for (int i = 0; i < 4; i ++) {
            UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(self.width/4*i, 0, self.width/4, _headView.height) title:titleArr[i] titleColor:kColorHEX(0x333333) image:imgArr[i]];
            [itemBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleTop ImageTitleSpace:KScaleWidth(10.0)];
            [_headView addSubview:itemBtn];
        }
    }
    return _headView;
}

- (NSMutableArray *)msgModelArr {
    if (!_msgModelArr) {
        _msgModelArr = [NSMutableArray array];
    }
    return _msgModelArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
